#!npx coffee
CITY = require("./city.json")
console.log CITY
puppeteer = require('puppeteer')

do =>
  browser = await puppeteer.launch({
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage'
    ],
    timeout: 10000,
  })
  keyword = "唯品会"

  for city in CITY
    url = "https://#{city}.meituan.com/s/#{keyword}/"
    console.log url
    await page(browser, url)

  await browser.close()

page = (browser, url)=>

  page = await browser.newPage()
  await page.setRequestInterception(true)
  page.on(
    'request',
    (request) =>
      if request.resourceType() == 'image'
        request.abort()
      else
        request.continue()
  )
  try
    # networkidle0 等待直到500ms内没有请求了
    await page.goto(url, {
      waitUntil: 'networkidle0'
    })
    await page.waitForSelector('body')
  catch err
    console.error(err)
    throw err

  html = await page.content()
  console.log(html)
  await page.close()
